//
//  RunMain.m
//  Test
//
//  Created by lizq on 15/9/2.
//  Copyright (c) 2015年 lizq. All rights reserved.
//

#import "RunMain.h"
#import "RuntimeObject.h"
#import <objc/runtime.h>
#import "NSObject+Extend.h"
@implementation RunMain
- (void)testRunTime {
    RuntimeObject *instance = [[RuntimeObject alloc] init];
    
    unsigned int count;
    objc_property_t *propertyList = class_copyPropertyList([instance class], &count);
    for(unsigned i = 0; i< count;i++) {
        const char *propertyName = property_getName(propertyList[i]);
        NSLog(@"Property:%@",[NSString stringWithUTF8String:propertyName]);
    }
    
    Method *methodList = class_copyMethodList([instance class], &count);
    for(unsigned i = 0; i < count;i++) {
        Method method = methodList[i];
        SEL sel = method_getName(method);
        NSLog(@"Method:%@",NSStringFromSelector(sel));
    }
    
    Ivar *ivarList = class_copyIvarList([instance class], &count);
    for(unsigned i = 0; i < count;i++) {
        Ivar ivar = ivarList[i];
        const char *ivarName = ivar_getName(ivar);
        NSLog(@"Ivar:%@",[NSString stringWithUTF8String:ivarName]);
    }
    //调用一个不存在的方法
    [instance performSelector:@selector(click) withObject:nil];
    //调用方法
    [instance performSelector:@selector(getMessage) withObject:nil];
    
    NSLog(@"方法交换");
    SEL sel1 = @selector(goodIdea);
    SEL sel2 = @selector(getMessage);
    Method method1 = class_getInstanceMethod([self class], sel1);
    Method method2 = class_getInstanceMethod([self class], sel2);
    method_exchangeImplementations(method1, method2);
    [self getMessage];
    [self goodIdea];
    NSLog(@"方法替换");
    Method resetMethodOne = class_getInstanceMethod([self class], @selector(methodResetOne));
    Method resetMethodTwo = class_getInstanceMethod([self class],@selector(methodResetTwo));
    method_setImplementation(resetMethodOne,method_getImplementation(resetMethodTwo));
    [self methodResetOne];
}

- (void)getMessage {
    NSLog(@"%@ message",NSStringFromSelector(_cmd));
}

- (void)goodIdea {
    NSLog(@"%@ idea",NSStringFromSelector(_cmd));
}

- (void)methodResetOne {
    NSLog(@"%@: methodResetOne",NSStringFromSelector(_cmd));
}

- (void)methodResetTwo {
    NSLog(@"%@: methodResetTwo",NSStringFromSelector(_cmd));
}
@end
